package com.example.quiz.controller.admin;

import com.example.quiz.common.Result;
import com.example.quiz.model.Question;
import com.example.quiz.model.User;
import com.example.quiz.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/question")
public class QuestionManageController {

    @Autowired
    private QuestionService questionService;

    // 题目管理页面
    @GetMapping("/manage")
    public String managePage(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.isAdmin()) {
            return "redirect:/user/login";
        }
        return "admin/question/manage";
    }

    // 添加题目页面
    @GetMapping("/add")
    public String addPage(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.isAdmin()) {
            return "redirect:/user/login";
        }
        return "admin/question/add";
    }

    // 编辑题目页面
    @GetMapping("/edit/{id}")
    public String editPage(@PathVariable Long id, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.isAdmin()) {
            return "redirect:/user/login";
        }
        Question question = questionService.getById(id);
        model.addAttribute("question", question);
        return "admin/question/edit";
    }

    // 添加题目
    @PostMapping("/api/add")
    @ResponseBody
    public Result<Integer> addQuestion(@RequestBody Question question, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.isAdmin()) {
            return Result.error("无权限");
        }
        int result = questionService.add(question);
        if (result > 0) {
            return Result.success(result);
        }
        return Result.error("添加失败");
    }

    // 更新题目
    @PutMapping("/api/update")
    @ResponseBody
    public Result<Integer> updateQuestion(@RequestBody Question question, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.isAdmin()) {
            return Result.error("无权限");
        }
        int result = questionService.update(question);
        if (result > 0) {
            return Result.success(result);
        }
        return Result.error("更新失败");
    }

    // 删除题目
    @DeleteMapping("/api/delete/{id}")
    @ResponseBody
    public Result<Integer> deleteQuestion(@PathVariable Long id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.isAdmin()) {
            return Result.error("无权限");
        }
        int result = questionService.deleteById(id);
        if (result > 0) {
            return Result.success(result);
        }
        return Result.error("删除失败");
    }

    // 获取题目列表
    @GetMapping("/api/list")
    @ResponseBody
    public Result<Map<String, Object>> getQuestionList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Integer difficulty) {

        List<Question> questions = questionService.getPagedQuestions(page, size, categoryId, difficulty);
        int total = questionService.getTotalCount(categoryId,difficulty);

        Map<String, Object> data = new HashMap<>();
        data.put("list", questions);
        data.put("total", total);

        return Result.success(data);
    }
}
